package com.yougou.ui.address;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yougou.R;
import com.yougou.data.model.Address;

import java.util.ArrayList;
import java.util.List;

public class AddressListAdapter extends RecyclerView.Adapter<AddressListAdapter.ViewHolder> {
    
    private List<Address> mDataList = new ArrayList<>();
    private OnAddressActionListener mListener;
    
    public void setData(List<Address> dataList) {
        if (dataList != null) {
            mDataList = dataList;
            notifyDataSetChanged();
        }
    }
    
    public void setOnAddressActionListener(OnAddressActionListener listener) {
        mListener = listener;
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_address, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Address address = mDataList.get(position);
        
        // 姓名和电话
        holder.tvName.setText(address.getName());
        holder.tvPhone.setText(address.getPhone());
        
        // 详细地址
        holder.tvAddress.setText(address.getFullAddress());
        
        // 默认标签
        holder.tvDefault.setVisibility(address.isDefault() ? View.VISIBLE : View.GONE);
        
        // 设为默认按钮
        holder.tvSetDefault.setVisibility(address.isDefault() ? View.GONE : View.VISIBLE);
        holder.tvSetDefault.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onSetDefaultClick(address);
            }
        });
        
        // 编辑按钮
        holder.tvEdit.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onEditClick(address);
            }
        });
        
        // 删除按钮
        holder.tvDelete.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onDeleteClick(address);
            }
        });
        
        // 点击整个item
        holder.itemView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onAddressClick(address);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvPhone;
        TextView tvAddress;
        TextView tvDefault;
        TextView tvSetDefault;
        TextView tvEdit;
        TextView tvDelete;
        
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvPhone = itemView.findViewById(R.id.tv_phone);
            tvAddress = itemView.findViewById(R.id.tv_address);
            tvDefault = itemView.findViewById(R.id.tv_default);
            tvSetDefault = itemView.findViewById(R.id.tv_set_default);
            tvEdit = itemView.findViewById(R.id.tv_edit);
            tvDelete = itemView.findViewById(R.id.tv_delete);
        }
    }
    
    public interface OnAddressActionListener {
        void onAddressClick(Address address);
        void onEditClick(Address address);
        void onDeleteClick(Address address);
        void onSetDefaultClick(Address address);
    }
}

